<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;
    public $user;
    public $userId;
    public $model;
    public $auto;
    public function __construct($username,$password,$auto)
    {
        $this->username= $username;
        $this->password= $password;
        $this->auto = $auto;
    }

    public function authenticate()
    {
        $model = new LoginForm;
        $model->username = $this->username;
        $model->password = $this->password;
        if($model->validate()){
            $map = 'email=:username AND status=:status';
        }else{
            $error_info =$model->getErrors();
            if($error_info['username'] && $error_info['username'][0]=='noemail'){
                $map = 'username=:username AND status=:status';
            }else{
                return -99;
            }
        }
        $criteria=new CDbCriteria;
        $criteria->condition=$map;
        $criteria->params=array(':username'=>$this->username,':status'=>1);
        $user_info = User::model()->find($criteria);
        if($user_info === null){
              $this->errorCode = self::ERROR_USERNAME_INVALID;
              return false;
        }elseif($user_info->password !== sha1($this->password.$user_info->salt)){
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
            return false;
        }else{
            $this->errorCode=self::ERROR_NONE;
            $user_info->loginnum = $user_info->loginnum+1;
            $user_info->last_login_time = time();
            $user_info->last_login_ip = Xtutuquan::getClientIP();
            $auth = array(
                'id' =>$user_info->id,
                'username'=>$user_info->username,
                'last_login_time'=>time(),
                'avatar_status'=>$user_info->avatar_status,
                'avatar_public'=>$user_info->avatar_public,
                'bg_url'=>$user_info->bg,
                'nick_name'=>$user_info->nick_name,
                'feeling'=>$user_info->feeling,
                'info_status'=>$user_info->info_status,
                'is_domain'=>$user_info->is_domain,
                'domain'=>$user_info->domain,
            );
            $this->userId = $user_info->id;
            $count = $user_info->update();
            $group_id = $user_info->group_id;
            $message = Xtutuquan::getUserEmail($user_info->id,$group_id);
            $this->setState('message',$message);
            $event = Xtutuquan::getUserEvent($user_info->id);
            $this->setState('event',$event);
            $this->setState('group_id',$group_id);
            $this->setState('user_auth',$auth);
            $this->setState('user_role',$user_info->role);
            $this->setState('role_salt',$user_info->role_salt);
            if($this->auto == 'on'){
                Xtutuquan::_cookiesSet('auto',Xtutuquan::encryption($user_info->username.'|'.Xtutuquan::getClientIP()),time()+3600*24*30);
            }
            return !$this->errorCode;
        }
    }
    /**
     *
     * @return integer id of the logged user, null if not set
     */
    public function getId() {
        return $this->userId;
    }
}